﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DAL
{
    public class SellerRange
    {
        public static void insert(SellerRangeEntity sellerRange)
        {
            DbHelper.ExecuteNonQuery("p_SellerRange_insert",
              sellerRange.CompanyId,
              sellerRange.ProductId,
              sellerRange.Rate,
              sellerRange.businessNature
               );
        }
        
        public static void updateCancel(SellerRangeEntity sellerRange)
        {
            DbHelper.ExecuteNonQuery("p_SellerRange_updateCancel",sellerRange.SrId,sellerRange.IsCancel,sellerRange.AdminId);
        }

        public static SellerRangeEntity getById(int srId)
        {
            DataRow dr = DbHelper.ExecuteDataRow("p_SellerRange_getById", srId);
            SellerRangeEntity sr = new SellerRangeEntity();
            if (dr != null)
            {
                sr.SrId = srId;
                sr.CompanyId = Convert.ToInt32(dr["companyId"]);
                if (dr["CancelDt"] != DBNull.Value)
                    sr.CancelDt = Convert.ToDateTime(dr["CancelDt"]);
                sr.IsCancel = Convert.ToBoolean(dr["IsCancel"]);
                sr.ProductId = Convert.ToInt32(dr["ProductId"]);
                sr.Rate = Convert.ToDecimal(dr["Rate"]);
                
            }

            return sr;
        
        }

        public static void delete(int srId,int adminId)
        {
            DbHelper.ExecuteNonQuery("[dbo].[p_SellerRange_delete]",srId,adminId);
        }

        public static DataTable getProductByCompany(int companyId ,int categoryId)
        {
            return DbHelper.ExecuteDataTable("p_sellerRange_getProductByCompany",companyId,categoryId);
        }

        public static bool IsSupply(int companyId,int productId,string businessNature)
        {
            object obj = DbHelper.ExecuteScalar("[dbo].[p_SellerRange_getByCompanyAndProductIdAndBus]", companyId, productId, businessNature);
            return Convert.ToBoolean(obj);
        }

        public static DataTable list()
        {
            return DbHelper.ExecuteDataTable("p_SellerRange_list");
        }

        public static void deleteBySelected(string srIds, int sellerid,int adminId)
        {
            DbHelper.ExecuteNonQuery("p_SellerRange_DelBySelected", srIds, sellerid,adminId);
        }

        public static void insertBySelected(string productIds, int sellerid)
        {
            DbHelper.ExecuteNonQuery("p_SellerRange_InsertBySelected", productIds, sellerid);
        }

        public static DataTable getByCompanyId(int companyid)
        {
            return DbHelper.ExecuteDataTable("p_SellerRange_getByCompanyId",companyid);
        }

        public static DataSet sellerRangelistWithPrice(int companyId,string where,int pageIndex,int pageSize)
        {
            return DbHelper.ExecuteReDataSet("p_SellerRange_listWithPrice", companyId, where, pageIndex, pageSize);
        }

        public static DataSet snnightOrderOptimize(string where, string orderby, int pagesize, int pageindex)
        {
            return DbHelper.ExecuteReDataSet("p_SennightOrderOptimize", "productId,productNo,name,categoryId,unit", "vw_Product_list", where, "productId", orderby, pagesize, pageindex);
        }

        public static void setFavourite(int srId)
        {
            DbHelper.ExecuteNonQuery("[dbo].[p_SellerRange_setFavourite]", srId);
        }

        public static void sellerRangeBypricebusinessNature(int sellerId)
        {
            DbHelper.ExecuteNonQuery("[p_sellerRange_ByPricebusinessNatureTosellerRange]", sellerId);
        }

        public static DataSet exportBySellerId(int companyId)
        {
            return DbHelper.ExecuteReDataSet("[dbo].[p_SellerRange_ExportBySellerId]", companyId);
        }

        public static DataTable sellerRaneParentCategory(int companyID)
        {
            return DbHelper.ExecuteDataTable("P_category_select_sellerRaneParentCate", companyID);
        }

        public static DataTable sellerRangeChildCategory(int companyID, int parentID)
        {
            return DbHelper.ExecuteDataTable("P_category_select_SellerRange_smallCate", companyID, parentID);
        }
    }
}
